﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using NHibernate.Transform;
using Nissen_Model;
using Nissen_Model.Dto;

namespace Nissen_Dao
{
	public class WorkerDao : DaoBase
	{
		public IList<Worker> GetWorkerByWorkerSSO(string workerSSO)
		{
			using (ISession session = this.SessionFactory.OpenSession())
			{
				ICriteria crt = session.CreateCriteria<Worker>();

				crt.Add(Restrictions.Eq("WorkerSSO", workerSSO));

				return crt.List<Worker>();

			}

		}

		public void ClearAllWorker()
		{
			using (ISession session = this.SessionFactory.OpenSession())
			{
				using (ITransaction t1 = session.BeginTransaction())
				{

					session.CreateSQLQuery("Delete From T_Worker").ExecuteUpdate();
					
					t1.Commit();
				}

			}
		}


		public IList<WorkerMap> GetWorkerMap()
		{
			using (ISession session = this.SessionFactory.OpenSession())
			{
				StringBuilder sql = new StringBuilder();

				sql.Append(" SELECT tw.Worker_SSO WorkerSSO, ");
				sql.Append(" twc.WorkerName WorkerName, ");
				sql.Append(" tns.Nissen_SSO_Code NissenSSO ");
				sql.Append(" FROM T_Worker tw ");
				sql.Append(" LEFT JOIN T_Nissen_SSO tns ON tw.Nissen_SSO_ID = tns.ID ");
				sql.Append(" LEFT JOIN T_WorkerCache twc ON tw.Worker_SSO = twc.WorkerSSO ");

				return session.CreateSQLQuery(sql.ToString()).
					SetResultTransformer(Transformers.AliasToBean(typeof (WorkerMap)))
					.List<WorkerMap>();
			}
		}

	}
}
